<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="first">
            <el-row style="height:60px;">
              <el-button icon="el-icon-plus" size="small" type="primary" @click="addRole">新增角色</el-button>
            </el-row>
            <el-table
              border=""
              :data="list"
              style="width: 100%"
            >
              <el-table-column
                type="index"
                align="center"
                label="序号"

                width="120"
              />
              <el-table-column
                prop="name"
                label="名称"
                align="center"

                sortable
                width="240"
              />
              <el-table-column
                prop="description"
                label="描述"
                align="center"

                sortable
              />
              <el-table-column
                label="操作"
                align="center"
              >
                <template slot-scope="{row}">
                  <el-button
                    size="small"
                    type="success"
                    @click="assignPerm(row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="updateRole(row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="deleteRole(row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <!-- 放置分页组件 -->
            <el-row type="flex" justify="end" align="middle" style="height:60px;">
              <el-pagination
                :total="page.total"
                :page-size="page.pagesize"
                :current-page="page.page"
                layout="prev, pager, next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-alert
              title="对公司名称,公司地址,营业执照,公司地区的更新,将使公司资料被重新审核,请谨慎修改"
              type="info"
              :show-icon="true"
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top:50px; magrin-left:50px">
              <el-form-item label="企业名称">
                <el-input v-model="formData.name" disabled style="width:400px;" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px;" />
              </el-form-item>
              <el-form-item label="电话">
                <el-input v-model="formData.companyPhone" disabled style="width:400px;" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px;" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" disabled type="textarea" :rows="3" style="width:400px;" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>

    </div>
    <el-dialog title="编辑弹层" :visible="showDialog" @close="btnCancel">
      <el-form ref="validator" label-width="120px" :model="ruleForm" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="ruleForm.description" />
        </el-form-item>
      </el-form>

      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="end">

        <el-button @click="btnCancel">取消</el-button>
        <el-button type="primary" @click="btnOK(infoObj.id)">确定</el-button>

      </el-row>
    </el-dialog>

    <!-- 分配权限弹出层 -->
    <el-dialog title="分配权限" :visible.sync="showPermDialog">
      <!-- 权限是一棵树 -->
      <!-- 数据绑定组件 -->
      <el-tree ref="getCheckedKeys" node-key="id" :default-checked-keys="newlist" show-checkbox :check-strictly="true" default-expand-all :data="permData" :props="defaultProps" />
      <template #footer>
        <el-row type="flex" justify="center">
          <el-button type="primary" @click="btnRoleOk">确定</el-button>
          <el-button @click="btnCancels">取消</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole, addAssign } from '@/api/setting'
import { mapGetters } from 'vuex'
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeDate } from '@/utils'
export default {
  // name: 'DishBoardContainer',
  data() {
    return {
      ruleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }

        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      },
      activeName: 'first',
      list: [], // 承接数组
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 10,
        total: 0 // 记录总数
      },
      formData: {},
      showDialog: false,
      infoObj: {},
      showPermDialog: false, // 控制分配权限的显示隐藏
      permData: [], // 获取权限树形数据
      defaultProps: {
        children: 'children',
        label: 'name'
      }, // 定义显示字段的名称和子组件名称
      userId: null, // 记录当前分配权限的id
      newlist: [] // 记录当前权限代年的标识
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList() // 获取角色列表
    this.getCompanyInfo()
  },

  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    changePage(newPage) {
    // 当前点击的页码
      this.page.page = newPage // 当前页码赋值给最新页码
      this.getRoleList()
    },
    async getCompanyInfo() {
      const result = await getCompanyInfo(this.companyId)
      this.formData = result
    },
    deleteRole(id) {
      this.$confirm('您确定删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteRole(id)
        this.getRoleList()
        this.$message({

          type: 'success',
          message: '删除成功!'
        })
      })
    },
    async updateRole(id) {
      this.infoObj = await getRoleDetail(id)
      this.showDialog = true
      this.ruleForm = this.infoObj
      this.$refs.validator.resetFields()
    },
    btnCancel() {
      this.ruleForm = {
        name: '',
        description: ''
      }
      this.showDialog = false
      this.$refs.validator.resetFields()
    },
    btnOK(id) {
      this.$refs.validator.validate(async isOK => {
        if (isOK) {
          if (this.ruleForm.id) {
            await updateRole(this.ruleForm)
            this.$message.success('更新成功')
          } else {
            // 新增角色
            await addRole({
              name: this.ruleForm.name,
              description: this.ruleForm.description
            })
            this.$message.success('新增成功')
          }

          this.getRoleList()
          this.showDialog = false
          this.$refs.validator.resetFields()
        }
      })
    },
    addRole() {
      this.showDialog = true
    },
    async assignPerm(id) {
      // 获取权限点数据
      // 有id先记录下来
      this.permData = tranListToTreeDate(await getPermissionList(), '0')
      this.userId = id
      // 获取这个id的权限点
      const { permIds } = await getRoleDetail(this.userId)
      console.log(permIds)
      // this.permData.filter(item => item)
      this.newlist = permIds
      this.showPermDialog = true
    },
    btnCancels() {
      this.showPermDialog = false
    },
    async btnRoleOk() {
      try {
        await addAssign({
          id: this.userId,
          permIds: this.$refs.getCheckedKeys.getCheckedKeys()
        })
        this.$message.success('保存成功')
        this.newlist = []
        this.showPermDialog = false
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped>

</style>
<style >
.dashboard-container .app-container .el-tabs__item {
font-size: 18px;
}

</style>
